package cn.wolfcode.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.wolfcode.business.domain.Appointment;
import cn.wolfcode.business.domain.Statement;
import cn.wolfcode.business.mapper.AppointmentMapper;
import cn.wolfcode.business.mapper.StatementMapper;
import cn.wolfcode.business.vo.StatementItemVO;
import cn.wolfcode.common.utils.DateUtils;
import cn.wolfcode.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.StatementItemMapper;
import cn.wolfcode.business.domain.StatementItem;
import cn.wolfcode.business.service.IStatementItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 结算单明细Service业务层处理
 *
 * @author 白金之星
 * @date 2023-10-31
 */
@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementMapper statementMapper;

    /**
     * 查询结算单明细
     *
     * @param id 结算单明细主键
     * @return 结算单明细
     */
    @Override
    public StatementItem selectStatementItemById(Long id) {
        return statementItemMapper.selectStatementItemById(id);
    }

    /**
     * 查询结算单明细列表
     *
     * @param statementItem 结算单明细
     * @return 结算单明细
     */
    @Override
    public List<StatementItem> selectStatementItemList(StatementItem statementItem) {
        return statementItemMapper.selectStatementItemList(statementItem);
    }

    /**
     * 新增结算单明细
     *
     * @param statementItem 结算单明细
     * @return 结果
     */
    @Override
    public int insertStatementItem(StatementItem statementItem) {
        return statementItemMapper.insertStatementItem(statementItem);
    }

    /**
     * 修改结算单明细
     *
     * @param statementItem 结算单明细
     * @return 结果
     */
    @Override
    public int updateStatementItem(StatementItem statementItem) {
        return statementItemMapper.updateStatementItem(statementItem);
    }

    /**
     * 批量删除结算单明细
     *
     * @param ids 需要删除的结算单明细主键
     * @return 结果
     */
    @Override
    public int deleteStatementItemByIds(Long[] ids) {
        return statementItemMapper.deleteStatementItemByIds(ids);
    }

    /**
     * 删除结算单明细信息
     *
     * @param id 结算单明细主键
     * @return 结果
     */
    @Override
    public int deleteStatementItemById(Long id) {
        return statementItemMapper.deleteStatementItemById(id);
    }

    /**
     * 保存数据
     *
     * @param vo
     * @return
     */
    @Override
    public void StatementItem(StatementItemVO vo) {
        //参数校验
        if (vo == null || vo.getStatementId() == null) {
            throw new RuntimeException("参数异常");
        }
        //根据id查询数据
        Long statementId = vo.getStatementId();
        //校验是否是消费中，消费中才能保存
        Statement statement = statementMapper.selectStatementById(statementId);
        if (statement == null) {
            throw new RuntimeException("非法操作");
        }
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            throw new RuntimeException("状态必须为消费中");
        }

        //清空数据
        statementItemMapper.deleteByStatementId(statementId);
        //遍历循环
        List<StatementItem> statementItems = vo.getStatementItems();
        BigDecimal totalAmount = BigDecimal.ZERO;
        Long totalQuantity = 0L;
        for (StatementItem item : statementItems) {
            //计算总数量
            totalQuantity += item.getItemQuantity();
            //计算总金额
            totalAmount = totalAmount.add(item.getItemPrice().multiply(new BigDecimal(item.getItemQuantity())));
            //插入明细单
            statementItemMapper.insertStatementItem(item);
        }
        //判断优惠金额不能大于总金额
        if (vo.getDiscountPrice().compareTo(totalAmount) > 0) {
            throw new RuntimeException("优惠金额不能大于总金额");
        }
        appointmentMapper.updateAuditStatus(statement.getAppointmentId(),Appointment.STATUS_SETTLE);
        statementMapper.updateAmount(statementId, totalAmount, totalQuantity, vo.getDiscountPrice());


    }

    /**
     * 支付结算
     *
     * @param id
     */
    @Override
    public void pay(Long id) {
        //参数校验
        if (id == null) {
            throw new RuntimeException("参数异常");
        }
        //判断状态必须是消费中才能修改
        Statement statement = statementMapper.selectStatementById(id);
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            throw new RuntimeException("必须是消费中才能修改");
        }
        Long userId = SecurityUtils.getUserId();
        statementMapper.updateStatus(id, userId, DateUtils.getNowDate(), Statement.STATUS_PAID);
        //判断是否是养修预约生成的结算单, 如果是修改状态为已支付状态
        if (statement.getAppointmentId() != null) {
            appointmentMapper.updateAuditStatus(statement.getAppointmentId(), Appointment.STATUS_PAYED);
        }
    }
}